#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace XnaGameComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Ship : ModelViewer
    {
        private bool engineStarted;
        private float engineStartedPosition;
        private float engineStoppedPosition;
        private bool doEngineAction;

        public Ship(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
            engineStartedPosition = position.Y+300;
            engineStoppedPosition = position.Y;
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            base.Update(gameTime);
            IGameConfiguration config = (IGameConfiguration)Game.Services.GetService(typeof(IGameConfiguration));
            if (Keyboard.GetState().IsKeyDown(config["Engine"]))
                doEngineAction = true;
            if (!engineStarted && doEngineAction)
                StartEngine();
            else if(doEngineAction)
                StopEngine();
        }

        private void StopEngine()
        {
            float newy=(float)Math.Log(position.Y-engineStoppedPosition);
            position.Y-=newy;
            if (newy==0)
            {
                doEngineAction = false;
                engineStarted = false;
            }
        }

        private void StartEngine()
        {

            float newy=(float)Math.Log(engineStartedPosition-position.Y);
            position.Y += newy;
            if (newy==0)
            {
                doEngineAction = false;
                engineStarted = true;
            }
        }
    }
}


